import { getPageColumns } from '@/utils/auth'
const memoryColumnsMixin = {
  data() {
    return {
      columns: [],
      pageName: ''
    }
  },
  created() {
    this.initLocalColumnsSetting();
  },
  methods: {
    initLocalColumnsSetting() {
      const localColumns = getPageColumns(this.$store.getters.username + '_' + this.pageName)
      if (localColumns.length === this.columns.length) {
        this.columns = localColumns
      } else if (localColumns.length > this.columns.length) {
        const newLocal = localColumns.filter(function(objA) {
          return this.columns.some(function(objB) {
            return objA.key === objB.key;
          });
        });
        this.columns = newLocal
      } else {
        this.columns.forEach(function(objB) {
          var existsInA = localColumns.some(function(objA) {
            return objA.key === objB.key;
          });
          if (!existsInA) {
            localColumns.push(objB);
          }
        });
        this.columns = localColumns
      }
    },
    isColumnVisible(field) {
      var targetCol = this.columns.filter(col => col.key === field)
      return targetCol[0]?.visible
    }
  }
}

export default memoryColumnsMixin
